package com.example.networkinteraction.customclient;

import com.example.networkinteraction.interceptor.HttpLoggingInterceptor;
import com.example.networkinteraction.util.HttpUtils;
import okhttp3.OkHttpClient;

import java.util.concurrent.TimeUnit;
import java.util.logging.Level;

import static com.example.networkinteraction.constant.NetworkContant.*;

/**
 * Description: 定制化的OkHttpClient
 * File: CustomOkHttpClient.java
 * Author: Administrator
 * Create: 2019/3/26 0026 上午 9:41
 */
public enum CustomOkHttpClient {
    INSTANCE;
    private OkHttpClient okHttpClient;

    {
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(TAG);
        loggingInterceptor.setPrintLevel(HttpLoggingInterceptor.Level.BASIC);
        loggingInterceptor.setColorLevel(Level.INFO);
        builder.addInterceptor(loggingInterceptor);

        builder.readTimeout(TIME_OUT, TimeUnit.SECONDS);
        builder.writeTimeout(TIME_OUT, TimeUnit.SECONDS);
        builder.connectTimeout(TIME_OUT, TimeUnit.SECONDS);

        HttpUtils.SSLParams sslParams = HttpUtils.getSslSocketFactory();
        builder.sslSocketFactory(sslParams.sSLSocketFactory, sslParams.trustManager);
        builder.hostnameVerifier(HttpUtils.UnSafeHostnameVerifier);
        okHttpClient = builder.build();
    }

    public OkHttpClient getOkHttpClient() {
        return okHttpClient;
    }

    public static CustomOkHttpClient getInstance() {
        return INSTANCE;
    }
}
